﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 备件到货记录录入 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int id = 0;

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }

                if (id > 0)
                {
                    this.DHID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.DHID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.NianDu.Text = d.Year.ToString();
                    this.YueFen.Text = d.Month.ToString();
                    this.Date1.Text = d.ToLongDateString();
                }
            }
            this.Label_GuiGe.Text = this.GuiGe.Value;
            this.Label_DanWei.Text = this.DanWei.Value;
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.BPID.Value = ((int)e.Command.Parameters[1].Value).ToString();
                this.NianDu.Text = ((short)e.Command.Parameters[2].Value).ToString();
                this.YueFen.Text = ((byte)e.Command.Parameters[3].Value).ToString();
                this.Date1.Text = ((DateTime)e.Command.Parameters[4].Value).ToLongDateString();
                this.ShuLiang.Text = ((int)e.Command.Parameters[5].Value).ToString();
                this.DanJia.Text = ((double)e.Command.Parameters[6].Value).ToString();
                this.ChangShang.Text = (string)e.Command.Parameters[7].Value;
                this.JingBan.Text = (string)e.Command.Parameters[8].Value;
                this.BaoGuan.Text = (string)e.Command.Parameters[9].Value;
                this.BeiZhu.Text = (string)e.Command.Parameters[10].Value;
                this.PinMing.Text = (string)e.Command.Parameters[11].Value;
                this.GuiGe.Value = (string)e.Command.Parameters[12].Value;
                this.DanWei.Value = (string)e.Command.Parameters[13].Value;
                this.Label_GuiGe.Text = this.GuiGe.Value;
                this.Label_DanWei.Text = this.DanWei.Value;

            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.BPID.Value == "0")
            {
                this.Message.Text = "·备品备件品种录入有误";
                return; 
            }

            int shuliang;
            if (!int.TryParse(this.ShuLiang.Text, out shuliang) || shuliang <= 0)
            {
                this.Message.Text = "·“数量”录入有误，请录入一个正整数";
                return; 
            }
            decimal danjia;
            if (!decimal.TryParse(this.DanJia.Text, out danjia) || danjia <= 0)
            {
                this.Message.Text = "·“单价”录入有误";
                return; 
            }

            DateTime d1;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Message.Text = "·“到货日期”录入有误";
                return; 
            }
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            //开始保存数据
            if (this.DHID.Text =="0")
            {
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["类别ID"].DefaultValue = this.BPID.Value;
                pc["计划年度"].DefaultValue = this.NianDu.Text;
                pc["计划月份"].DefaultValue = this.YueFen.Text;
                pc["日期"].DefaultValue = d1.ToShortDateString();
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["厂商"].DefaultValue = this.ChangShang.Text;
                pc["经办人"].DefaultValue = this.JingBan.Text;
                pc["保管员"].DefaultValue = op;
                pc["备注"].DefaultValue = this.BeiZhu.Text;

                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
            else
            {
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["ID"].DefaultValue = this.DHID.Text;
                pc["类别ID"].DefaultValue = this.BPID.Value;
                pc["计划年度"].DefaultValue = this.NianDu.Text;
                pc["计划月份"].DefaultValue = this.YueFen.Text;
                pc["日期"].DefaultValue = d1.ToShortDateString();
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["厂商"].DefaultValue = this.ChangShang.Text;
                pc["经办人"].DefaultValue = this.JingBan.Text;
                pc["保管员"].DefaultValue = op;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[0].Value))
            {
                this.DHID.Text = ((int)e.Command.Parameters[0].Value).ToString();
            }
        }

        protected void Button_ReNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("备件到货记录录入.aspx");
        }
    }
}
